package com.wang.server.netty;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.util.IOUtils;
import com.wang.rest.RestServer;
import com.wang.server.netty.handler.BizResultHandler;
import com.wang.server.netty.handler.LoggerHandler;
import com.wang.server.netty.handler.RestfulServerHandler;
import com.wang.utils.DatabaseUtils;
import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelOption;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.handler.codec.http.HttpObjectAggregator;
import io.netty.handler.codec.http.HttpServerCodec;

import java.io.FileNotFoundException;
import java.io.FileReader;
import java.sql.Connection;

//Netty服务器端
public class NettyServer {
    private int port;

    public NettyServer(int port) {
        this.port = port;
    }

    private void bind() throws InterruptedException, FileNotFoundException {
        // 服务器启动
        EventLoopGroup bossGroup = new NioEventLoopGroup();//用于服务器端接受客户端的连接
        EventLoopGroup workGroup = new NioEventLoopGroup();//用于网络事件的处理
        final RestfulServerHandler restfulServerHandler = restfulServerHandler();
        try {
            new ServerBootstrap().group(bossGroup, workGroup).channel(NioServerSocketChannel.class).childHandler(new ChannelInitializer<SocketChannel>() {
                @Override
                protected void initChannel(SocketChannel ch) {
                    ch.pipeline().addLast(new HttpServerCodec());// http 编解码
                    ch.pipeline().addLast("httpAggregator", new HttpObjectAggregator(512 * 1024)); // http 消息聚合器
                    ch.pipeline().addLast(new LoggerHandler());
                    ch.pipeline().addLast(restfulServerHandler);
                    ch.pipeline().addLast(new BizResultHandler());
//                    ch.pipeline().addLast(new SecondOutChannel());
                }
            }).option(ChannelOption.SO_BACKLOG, 1024)
                    .bind(port).sync()
                    .channel().closeFuture().sync();//指定此套接口排队的最大连接个数
        } finally {
            bossGroup.shutdownGracefully();
            workGroup.shutdownGracefully();
        }
    }

    private RestfulServerHandler restfulServerHandler() throws FileNotFoundException {
        // 启动sql服务
        String config = IOUtils.readAll(new FileReader("D:\\waveproject\\code_tool_library\\src\\main\\resource\\real_curd\\config.json"));
        // 初始化
        Connection connection = DatabaseUtils.INSTANCE.openConnection(JSONObject.parseObject(config).getJSONObject("database"));
        return new RestfulServerHandler("api/wdj/", connection);
    }

    private void startServers() throws FileNotFoundException {
    }

    public static void main(String[] args) throws InterruptedException, FileNotFoundException {
        new NettyServer(8080).bind();
    }
}